//editables on first profile page
$.fn.editable.defaults.mode = 'inline';
$.fn.editableform.loading = "<div class='editableform-loading'><i class='light-blue icon-2x icon-spinner icon-spin'></i></div>";
$.fn.editableform.buttons = '<button type="submit" class="btn btn-info editable-submit"><i class="icon-ok icon-white"></i></button>' +
    '<button type="button" class="btn editable-cancel"><i class="icon-remove"></i></button>';

var merchanInfo;//商户信息
var merchantId = $("#merchantId").val();

initMerchanInfo();

function initMerchanInfo() {
    if (cochoApp.isNotEmpty(merchantId)) {
        getMerchanInfo();
        if (cochoApp.isNotEmpty(merchanInfo)) {
            $("#mchntid").val(merchanInfo.mchntid);
            $("#mchntid").attr("disabled",true);
            $("#merchantName").val(merchanInfo.merchantName);
            $("#merchantName").attr("disabled",true);
            $("#inscd").val(merchanInfo.inscd);
            $("#backUrl").val(merchanInfo.backUrl);
            $("#version").val(merchanInfo.version);
            $("#signType").val(merchanInfo.signType);
            $("#charset").val(merchanInfo.charset);
            $("#signKey").val(merchanInfo.signKey);
            $("#isPay").val(merchanInfo.isPay);
            $("#usagetags").val(merchanInfo.usagetags);
            $("#acctNo").val(merchanInfo.acctNo);
            $("#acctType").val(merchanInfo.acctType);
            $("#chName").val(merchanInfo.chName);
            $("#custmrtp").val(merchanInfo.custmrtp);
            $("#custmracnt").val(merchanInfo.custmracnt);
            $("#phoneNum").val(merchanInfo.phoneNum);
            $("#chBank").val(merchanInfo.chBank);
            $("#bankNo").val(merchanInfo.bankNo);
            $("#mchntid2").val(merchanInfo.mchntid2);
        }
    }
}

function getMerchanInfo() {
    $.ajax({
        type: "post",
        async: false,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/admin/merchant/getMerchantInfoById.do?token=" + cochoApp.getTokenByStorage() + "&merchantId=" + merchantId,
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    merchanInfo = data.dataRows[0];
                } else {
                    cochoAlert.notificationsWarn("暂无商户信息！");
                }
            } else {
                cochoAlert.notificationsWarn("获取商户信息失败！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

var agencyList;//代理机构列表
initAgencySelect();
function initAgencySelect() {
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/members/common/getAgencyInfo.do?token=" + cochoApp.getTokenByStorage(),
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    agencyList = data.dataRows;
                    for (var i = 0; i < agencyList.length; i++) {
                        var agencyInfo = agencyList[i];
                        if (merchanInfo != null && agencyInfo.inscd == merchanInfo.inscd) {
                            $("#inscd").append("<option selected='selected' value='" + agencyInfo.inscd + "'>" + agencyInfo.name + "</option>");
                        } else {
                            $("#inscd").append("<option value='" + agencyInfo.inscd + "'>" + agencyInfo.name +  "</option>");
                        }
                    }
                    $("#inscd").chosen();
                } else {
                    cochoAlert.notificationsWarn("暂无可选信息！");
                }
            } else {
                cochoAlert.notificationsWarn("暂时无法从服务器获取信息，请重新登录或联系管理员！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

function saveMerchantInfo() {
    var result = checkNullAndWarnHasNull();
    if (result == true) {//如果必填项都不为空
        var merchantId = $("#merchantId").val();
        var mchntid = $("#mchntid").val();
        var merchantName = $("#merchantName").val();
        var inscd = $("#inscd").val();
        var backUrl = $("#backUrl").val();
        var txndir = $("#txndir").val();
        var version = $("#version").val();
        var signType = $("#signType").val();
        var charset = $("#charset").val();
        var signKey = $("#signKey").val();
        var isPay = $("#isPay").val();
        var usagetags = $("#usagetags").val();
        var acctNo = $("#acctNo").val();
        var acctType = $("#acctType").val();
        var chName = $("#chName").val();
        var custmrtp = $("#custmrtp").val();
        var custmracnt = $("#custmracnt").val();
        var phoneNum = $("#phoneNum").val();
        var chBank = $("#chBank").val();
        var bankNo = $("#bankNo").val();
        var mchntid2 = $("#mchntid2").val();
        var paramData = {"merchantId":merchantId,"mchntid":mchntid,"merchantName": merchantName,"inscd": inscd,"backUrl":backUrl,"txndir":txndir,"version":version,"signType":signType,"charset":charset,"signKey":signKey,"isPay":isPay,"usagetags":usagetags,"acctNo":acctNo,"acctType":acctType,"chName":chName,"custmrtp":custmrtp,"custmracnt":custmracnt,"phoneNum":phoneNum,"chBank":chBank,"bankNo":bankNo,"mchntid2":mchntid2, "token": cochoApp.getTokenByStorage()};

        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/admin/merchant/saveMerchantInfo.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    cochoAlert.notificationsWarn(data.statusMsg);
                    $("#merchantId").val(data.content);
                } else {
                    cochoAlert.notificationsWarn("保存商户信息失败！");
                }
            },
            error: function (data) {
                cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
            }
        });
    }
}
